package ch02.impl;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @auth 邝志豪
 * @date 2024/1/5 15:50
 */
public class Ac3Impl {
    public static void main(String[] args) {
        Document doc=null;
        try{
            //加载DOM树
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("test/ch02/acXml/ac3.xml"));
        }catch(Exception e){
            e.printStackTrace();
        }

        //获取XML的根节点
        Element root = doc.getRootElement();
        //遍历所有Brand标签
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();){
            Element brand=(Element) itBrand.next();
            //输出标签name属性
            System.out.println("品牌："+brand.attributeValue("name"));
            //遍历Type标签
            for (Iterator itType = brand.elementIterator(); itType.hasNext();){
                Element type=(Element) itType.next();
                //输出表亲的name属性
                System.out.println("\t型号："+type.attributeValue("name"));
            }
        }
    }
    @Test
    public void testUpdate() throws TransformerException, IOException {
        Document doc=null;
        try{
            //加载DOM树
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("test/ch02/acXml/ac3.xml"));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        Element rootElement=doc.getRootElement();
        List nodes=rootElement.elements("Brand");

        for(Iterator iterator=nodes.iterator();iterator.hasNext();){
            Element elm=(Element) iterator.next();
            Attribute attribute=elm.attribute("name");
            String brandName=attribute.getText();
            if(brandName.equals("三星")){
                elm.addAttribute("name","SAMSUNG");   //修改属性
            }
        }
        XMLWriter writer=new XMLWriter(new FileWriter("test/ch02/acXml/ac3.xml"));
        writer.write(doc);
        writer.close();
    }
    @Test
    public void testDelete() throws IOException {
        Document doc=null;
        try{
            //加载DOM树
            SAXReader saxReader=new SAXReader();
            doc=saxReader.read(new File("test/ch02/acXml/ac3.xml"));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        Element rootElm = doc.getRootElement();
        List<Element> nodes=rootElm.elements("Brand");
        for (Iterator it = nodes.iterator(); it.hasNext();) {
            Element brandElement = (Element) it.next();
            String brandName = brandElement.attributeValue("name"); // 取得Brand元素的name属性的值
            if (brandName.equals("SAMSUNG")) {
                // 使用迭代器的remove()方法删除Brand元素
                it.remove();
            }
        }
        XMLWriter writer=new XMLWriter(new FileWriter("test/ch02/acXml/ac3.xml"));
        writer.write(doc);
        writer.close();
    }
}
